<?php

namespace App\Http\Controllers;

use App\Repositories\PermissionRepository;
use App\Http\Requests\CreatePermissionRequest;
use App\Http\Requests\UpdatePemissionRequest;
use App\Repositories\RoleRepository;
use Toastr;

class PermissionController extends Controller
{
    protected $permission, $role;
    protected $fields = [
        'name' => '',
        'display_name' => '',
        'description' => '',
        'is_menu' => 0,
        'icon' => '',
        'sort' => 0,
        'pid' => 0,
    ];

    public function __construct(PermissionRepository $permission, RoleRepository $role)
    {
        parent::__construct();
        $this->permission = $permission;
        $this->role = $role;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $permissions = buildPermission($this->permission->getAll());
        return view('permission.index', compact('permissions'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        foreach ($this->fields as $field => $value) {
            $data[$field] = old($field, $value);
        }
        return view('permission.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \App\Http\Requests\CreatePermissionRequest $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(CreatePermissionRequest $request)
    {
        if ($this->permission->create($request->all())) {
            Toastr::success('添加成功');
            return redirect('/permission');
        }
        Toastr::error('添加失败');
        return redirect('/permission')->withErrors('添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $permission = $this->permission->find($id);
        $data['id'] = $id;
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $permission->$field);
        }
        return view('permission.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \App\Http\Requests\UpdatePemissionRequest $request
     * @param int                                       $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(UpdatePemissionRequest $request, $id)
    {
        $data = $request->all();
        if ($this->permission->update($data, $id)) {
            Toastr::success('更新成功');
            return redirect('/permission');
        }
        Toastr::error('更新失败');
        return redirect('/permission');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $child = $this->permission->findByField('pid', $id);
        if ($child) {
            Toastr::error('请先将该权限的子权限删除后再做删除操作!');
            return redirect()->back();
        }
        if ($this->permission->delete($id)) {
            Toastr::success('删除成功');
            return redirect('/permission');
        }
        Toastr::error('删除失败');
        return redirect('/permission');
    }
}
